package com.hqyj.emb.system.controller;

import com.hqyj.emb.system.model.User;
import com.hqyj.emb.system.model.User2;
import frame.controller.BaseController2;
import frame.model.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/userController.do")
public class UserController extends BaseController2 {
    public int getPC(HttpServletRequest req, HttpServletResponse resp) {

        String value = req.getParameter("pc");
        if (value == null || value.trim().isEmpty()) {
            return 1;
        }
        return Integer.parseInt(value);

    }
    public String query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("------------listPage()---");
        // a、 获取页面传递的pc:当前页码（page code）
        int pc = getPC(req, resp);
        // b、给定ps的值:每页记录数（page size）
        int ps = 6;

        // 3、调用业务
        PageBean<User> pageBean = userService.queryAllPage(pc, ps);
        req.setAttribute("pb", pageBean);

        //2.跳转页面

        return "view/system/user/userList.jsp";

    }
    public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String sex=req.getParameter("sex");
        String address = req.getParameter("address");
        String tel = req.getParameter("tel");
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        User user = new User();
        user.setName(name);
        user.setSex(sex);
        user.setAdress(address);
        user.setTel(tel);
        user.setUserName(userName);
        user.setPassword(password);
        int n=userService.addUser(user);

        List<User> userList=userService.queryUserByDb();
        req.setAttribute("userList",userList);
        return "userController.do?method=query";
    }
    public String delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userIdSTR");
        int userIdST=Integer.parseInt(userId);
        int d=userService.deleteUserByUser(userIdST);
        
        List<User> userList = userService.queryUserByDb();
        req.setAttribute("userList",userList);
        return "userController.do?method=query";
    }
    public String updateUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userIdSTR = req.getParameter("userIdSTR");
        int userId=Integer.parseInt(userIdSTR);
        User user_db= userService.queryUserByUserId(userId);

        req.setAttribute("user_db", user_db);
        System.out.println(user_db);
        return "view/system/user/updateUser.jsp";

    }
    public String update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String userIdSTR = req.getParameter("userIdSTR");
        int userId=Integer.parseInt(userIdSTR);
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String address = req.getParameter("address");
        String tel = req.getParameter("tel");
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        //封裝
        User user = new User();
        user.setUserId(userId);
        user.setName(name);
        user.setSex(sex);
        user.setAdress(address);
        user.setTel(tel);
        user.setUserName(userName);
        user.setPassword(password);
        System.out.println(user);
        int n=userService.updateUser(user);
        List<User> userList = userService.queryUserByDb();

        req.setAttribute("userList",userList);

        return "userController.do?method=query";
    }

    public String PLDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String checkId = req.getParameter("checkId");
        System.out.println(checkId);
        String[] split = checkId.split(",");
        int[] ids=new int[split.length];
        if(split.length>0){
            for(int i=0;i<split.length;i++){
                ids[i]=Integer.parseInt(split[i]);
            }
        }
        for (int id : ids) {
            int i = userService.deleteUserByUser(id);
        }
        List<User> userList=userService.queryUserByDb();

        req.setAttribute("userList",userList);
        return "userController.do?method=query";
    }
}
